<?php

class ErrorController extends Zend_Controller_Action
{
	public function init()
	{
		// ...
	}
	
  	public function errorAction()
  	{
  	    // Their could already be some rendered output in the response.
  	    $this->getResponse()->clearBody();
  	    
  		$errors = $this->_getParam('error_handler');
  		
  		
  		// Test if we should return the error as json instead
  		if(!$this->_request->isXmlHttpRequest()) {
  			
  			switch ($errors->type) {
	  			
	  			default: 
	  				$this->view->exception = $errors->exception;
	  				$this->view->request = $errors->request;
	  				break;
	  		}
	  		
  		} else {
  			
  			$this->_helper->viewRenderer->setNoRender();
  			
  			$message  = "Exception:\n\n";
  			$message .= "\tClass: " . get_class($errors->exception) . "\n";
  			$message .= "\tMessage: " . $errors->exception->getMessage() . "\n\n";
  			$message .= "Stacktrace:\n";
  			$message .= $errors->exception->getTraceAsString();

  			echo $this->_helper->jsonError($message);
  		}
  	}
}
